package com.dy.controller;

import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.common.R;
import com.dy.entity.Category;
import com.dy.service.CategoryService;
import com.dy.service.impl.CategoryServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:CategoryController
 * Package:com.dy.controller
 * Description:
 *
 * @Date:2022/8/4 8:56
 * @Author:dy
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("新增分类");

        String name = category.getName();
        Category one = categoryService.getCategoryByName(name);

        //判断 name 是否已存在
        if(one != null){
            return R.error("【"+name+"】已经存在，添加失败。(┬＿┬)");
        }

        //新增分类
        boolean save = categoryService.save(category);
        if(BooleanUtil.isTrue(save)){
            return R.success("新增分类成功。(^▽^ )");
        }
        log.info("新增分类失败");
        return R.error("添加失败,请重试。");
    }

    /**
     * @Author dy
     * @Description: 分页查找 菜品/套餐 分类
     */
    @GetMapping("/page")
    public R<Page<Category>> page(Integer page, Integer pageSize){
        Page<Category> categoryPage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);

        categoryService.page(categoryPage,queryWrapper);
        return R.success(categoryPage);
    }

    /**
     * @Author dy
     *  删除分类
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        return categoryService.delete(ids);
    }



    /**
     * @Author dy
     * @Description: 修改分类
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        // 确保修改的分类名，没重复
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        String name = category.getName();
        queryWrapper.eq(StringUtils.isNotBlank(name),Category::getName,name);
        Category one = categoryService.getOne(queryWrapper);

        if(one != null){
            return R.error("【"+name+"】已经存在，修改失败。(┬＿┬)");
        }

        boolean b = categoryService.updateById(category);
        if(BooleanUtil.isTrue(b)){
            return R.success("修改成功");
        }
        return R.error("修改失败。");
    }


    @GetMapping("/list")
    public R<List<Category>> list (int type){
        LambdaQueryWrapper<Category> queryWrapper ;
        if(type > 0){
            queryWrapper = new LambdaQueryWrapper<>();
            //添加 种类 类型过滤
            queryWrapper.eq(Category::getType,type);
            //添加排序条件
            queryWrapper.orderByAsc(Category::getSort);
            List<Category> list = categoryService.list(queryWrapper);
            return R.success(list);
        }
        log.error("动态下拉框传入的值不对。");
        return R.error("服务错误！");
    }


    @GetMapping("/clist")
    public R<List<Category>> list (){
        LambdaQueryWrapper<Category> queryWrapper ;
        queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort);
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }
}
